package com.xuemeng.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ScheduledFuture;


/**
 * @author xuemeng
 * @date 2018/12/13 17:39
 */
@Component
public class ScheduleConfig {
        @Autowired
        private ThreadPoolTaskScheduler threadPoolTaskScheduler;

        @Bean
        public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
                return new ThreadPoolTaskScheduler();
        }

        private ScheduledFuture<?> future;
        private String cron = "";

        public String getCron() {
                return cron;
        }

        public void stopCron() {
                if (future != null) {
                        //取消定时任务
                        future.cancel(true);
                }
        }

        public void setCron(String cron) {
                this.cron = cron;
                stopCron();
                future = threadPoolTaskScheduler.schedule(new Runnable() {
                        @Override
                        public void run() {
                                try {
                                        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                }, new Trigger() {
                        @Override
                        public Date nextExecutionTime(TriggerContext triggerContext) {
                                if ("".equals(cron) || cron == null) {
                                        return null;
                                } // 定时任务触发，可修改定时任务的执行周期
                                CronTrigger trigger = new CronTrigger(cron);
                                Date nextExecDate = trigger.nextExecutionTime(triggerContext);
                                return nextExecDate;
                        }
                });
        }
}


